﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.CodeDom;
using System.Collections;

using YT.Modules;
using YT.DB;
using YT.Commons;

namespace YT.XH.MeniereDisease
{
    public partial class frmMDTHITitle : Form
    {       
        private string mstrSickRecID = string.Empty;
        private DBAccess mdbcon = new DBAccess(false, false);
        private string mstrSeqNo = string.Empty;
        private string mstrHdrTableName = "";
        private ArrayList marrlist = new ArrayList();
        private FormStatus mfrmStatus;
        private string mstrResultTableName = "THIResult";
        private JobocoType mJobocoType = new JobocoType();
        private bool blnExit = false;
        public frmMDTHITitle()
        {
            InitializeComponent();
            mstrSickRecID = SysVar.SickRecID;
        }

        public frmMDTHITitle(string strSickRecID)
        {
            InitializeComponent();
            mstrSickRecID = strSickRecID;
        }
        public frmMDTHITitle(string strSickRecID,string strSeqNo)
        {
            InitializeComponent();
            mstrSickRecID = strSickRecID;
            mstrSeqNo = strSeqNo;
            mJobocoType = JobocoType.TreatEffect; 
        }

        public frmMDTHITitle(string strSickRecID, string strSeqNo, JobocoType jobocoType)
        {
            InitializeComponent();
            mstrSickRecID = strSickRecID;
            mstrSeqNo = strSeqNo;
            mJobocoType = jobocoType;
        }

        private void FindPanel(Control cons)
        {
            foreach (Control con in cons.Controls)
            {
                if (con.GetType().Name == "Panel" && con.Tag != "" && con.Tag != null)
                {
                    marrlist.Add(con);
                }
                if (con.Controls.Count > 0)
                {
                    FindPanel(con);
                }
                if (con.GetType().Name == "RadioButton") ((RadioButton)con).CheckedChanged += new System.EventHandler(this.Calulate);
            }
        }

        private void FillData()
        {
            DBAccess dbcon = new DBAccess(false, false);
            foreach (Control cons in this.Controls)
            {
                FindPanel(cons);
            }
            foreach (Control conDtl in marrlist)
            {
                if (conDtl.GetType().Name == "Panel")
                {
                    string[] strInfoSeqNo = new string[2];
                    strInfoSeqNo = conDtl.Tag.ToString().Split('.');
                    string strHdrSeqNo = strInfoSeqNo[0];
                    string strDtlSeqNo = strInfoSeqNo[1];
                    string strSickRecID = mstrSickRecID;
                    string strColTitle = string.Empty;
                    string strResult = string.Empty;
                    string strTitleType = TitleType.SuddenDeafness.ToString();
                    foreach (Control conText in conDtl.Controls)
                    {
                        if (conText.Tag != "" && conText.Tag != null)
                        {
                            strColTitle = conText.Tag.ToString();
                            string strSQL = "select top 1 Result from " + mstrResultTableName + " where rowno='" + strColTitle + "' and  HdrSeqNo='" + strHdrSeqNo + "' and DtlSeqNo='" + strDtlSeqNo + "' and Type='" + TitleType.MeniereDisease   + "' and ExamSeqNo='"+ mstrSeqNo +"' and SickRecID='"+ mstrSickRecID +"' and FromModule='"+ mJobocoType +"'";
                            DataRow drCheck = dbcon.GetDataFirstRow(strSQL);
                            if (drCheck == null) continue;
                            strResult = CvtFunc.ToString(drCheck["Result"]);
                            if (conText.GetType().Name == "TextBox")
                            {
                                conText.Text = strResult;
                            }
                            else if (conText.GetType().Name == "RadioButton")
                            {
                                if (strResult == "false")
                                    ((RadioButton)conText).Checked = false;
                                else if (strResult == "true")
                                    ((RadioButton)conText).Checked = true;
                            }
                            else if (conText.GetType().Name == "CheckBox")
                            {
                                if (strResult == "false")
                                    ((CheckBox)conText).Checked = false;
                                else if (strResult == "true")
                                    ((CheckBox)conText).Checked = true;
                            }
                            else if (conText.GetType().Name == "Label")
                            {
                                continue;
                            }
                        }
                    }

                }
            }
            dbcon.CloseCon();
        }
        public bool GetTotalJacobson(ref double dblTotalScore, ref double dblFscore, ref double dblEscore, ref double dblCscore, ref double dblSubScore)
        {
            this.ShowDialog();
            dblTotalScore = CvtFunc.ToDouble(txtTotalScore.Text);
            dblFscore = CvtFunc.ToDouble(txtFscore.Text);
            dblEscore = CvtFunc.ToDouble(txtEscore.Text);
            dblCscore = CvtFunc.ToDouble(txtCscore.Text);
            if (blnExit)
                return false ;
            else
                return true ;
        }
        private void UpdateData(string strExamSeqNo)
        {
            DBAccess dbcon = new DBAccess(false, false);
            foreach (Control conDtl in marrlist)
            {
                if (conDtl.GetType().Name == "Panel")
                {
                    string[] strInfoSeqNo = new string[2];
                    strInfoSeqNo = conDtl.Tag.ToString().Split('.');
                    string strHdrSeqNo = strInfoSeqNo[0];
                    string strDtlSeqNo = strInfoSeqNo[1];
                    string strSickRecID = mstrSickRecID;
                    string strColTitle = string.Empty;
                    string strResult = string.Empty;
                    DataRow drRow = null;
                    string strFields = "SickRecID,HdrSeqno,DtlSeqNo,RowNo,ColTitle,Result,Type,ExamSeqNo,FromModule";
                    foreach (Control conText in conDtl.Controls)
                    {
                        if (conText.Tag != "" && conText.Tag != null)
                        {
                            if (conText.GetType().Name == "TextBox")
                            {
                                strResult = conText.Text;
                            }
                            else if (conText.GetType().Name == "RadioButton")
                            {
                                strResult = (((RadioButton)conText).Checked ? "true" : "false");
                            }
                            else if (conText.GetType().Name == "CheckBox")
                            {
                                strResult = (((CheckBox)conText).Checked ? "true" : "false");
                            }
                            else if (conText.GetType().Name == "Label")
                            {
                                strResult = "";
                                continue;
                            }
                            strColTitle = conText.Tag.ToString();

                            string strSQL = "select top 1 * from " + mstrResultTableName + " where rowno='" + strColTitle + "' and  HdrSeqNo='" + strHdrSeqNo + "' and DtlSeqNo='" + strDtlSeqNo + "' and Type='" + TitleType.SuddenDeafness  + "' and ExamSeqNo='"+ mstrSeqNo +"' and SickRecID='"+ mstrSickRecID  +"' and FromModule='"+ mJobocoType  +"'";
                            bool blnIsNew = false;
                            DataRow drCheck = dbcon.GetDataFirstRow(strSQL);
                            if (drCheck == null)
                            {
                                blnIsNew = true;
                                drRow = dbcon.GetDataTable("select top 0 * from " + mstrResultTableName + " ").NewRow();
                            }
                            else
                            {
                                blnIsNew = false;
                                drRow = dbcon.GetDataFirstRow("select top 1 * from " + mstrResultTableName + " where recid='" + CvtFunc.ToString(drCheck["RecID"]) + "' ");
                            }

                            drRow["SickRecID"] = strSickRecID;
                            drRow["HdrSeqno"] = strHdrSeqNo;
                            drRow["DtlSeqNo"] = strDtlSeqNo;
                            drRow["RowNo"] = strColTitle;
                            drRow["Result"] = strResult;
                            drRow["Type"] = TitleType.MeniereDisease ;
                            drRow["FromModule"] = mJobocoType; 
                            drRow["ExamSeqNo"] = strExamSeqNo;
                            if (blnIsNew)
                                DataFunctions.UpdateTable(dbcon, strFields, mstrResultTableName, drRow, FormStatus.AddNew);
                            else
                                DataFunctions.UpdateTable(dbcon, strFields, mstrResultTableName, drRow, FormStatus.Update);
                        }
                    }
                }
            }
            dbcon.CloseCon();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            UpdateHdr();
            this.Close();
        }

        private void UpdateHdr()
        {
            string strSeqNo = string.Empty;
            DBAccess dbcon = new DBAccess(false, false);
            //string strSQL = string.Empty;
            //if (mfrmStatus == FormStatus.AddNew)
            //{
            //    strSeqNo = DataFunctions.GetFieldValue(mstrHdrTableName, "max(SeqNo) as SeqNo", "1=1 and Type='" + TitleType.MeniereDisease + "'");
            //    strSeqNo = CvtFunc.ToString(CvtFunc.ToInt(strSQL) + 1);
            //    strSQL = " Insert into " + mstrHdrTableName + " (  SickRecID, SeqNo, ExamDate,Type ) " +
            //             " values (  '" + mstrSickRecID + "','" + strSeqNo + "','" + BaseFunctions.FormatDate(DateTime.Now.Date.ToString(), DateFormatOption.DateFmtYMD) + "' " +
            //             " ,'" + TitleType.MeniereDisease + "' ) ";
            //    dbcon.ExecuteSql(strSQL);
            //}
            //else if (mfrmStatus == FormStatus.Update)
            //{
            //    strSeqNo = mstrSeqNo;
            //}
            UpdateData(mstrSeqNo);
            mfrmStatus = FormStatus.Update;
        }

        private void CalScoreAndSub()
        {
            int intResult = 0;
            int intFscore = 0;
            int intEscore = 0;
            int intPscore = 0;
            int intTotalScore = 0;
            int intTotalSubScore = 0;
            foreach (Control conDtl in marrlist)
            {
                if (conDtl.GetType().Name == "Panel")
                {
                    if (conDtl.Name.Substring(0, 4) == "pnl_")
                    {
                        string strType = conDtl.Name.Split('_')[1].Substring(0, 1);
                        foreach (Control conItem in conDtl.Controls)
                        {
                            if (conItem.GetType().Name == "RadioButton")
                            {
                                if (((RadioButton)conItem).Checked)
                                {
                                    intResult = BaseFunctions.GetTHIScoreFunction(conItem.Tag.ToString());
                                }
                                if (strType == "E") intEscore = intEscore + intResult;
                                if (strType == "F") intFscore = intFscore + intResult;
                                if (strType == "C") intPscore = intPscore + intResult;
                                intResult = 0;
                            }
                        }
                    }                    
                }
            }

            intTotalScore = intFscore + intEscore + intPscore;
            txtEscore.Text = CvtFunc.ToString(intEscore);
            txtCscore.Text = CvtFunc.ToString(intPscore);
            txtFscore.Text = CvtFunc.ToString(intFscore);

            txtTotalScore.Text = CvtFunc.ToString(intTotalScore);
            

        }

        
        private void btnCalu_Click(object sender, EventArgs e)
        {           
            CalScoreAndSub();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否保存？", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                UpdateHdr();
                this.Close(); 
            }
            else 
            {
                this.Close();
                blnExit = true;
            }
        }

        private void Calulate(object sender, EventArgs e)
        {
            CalScoreAndSub();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (this.Opacity >= 1)
                timer1.Stop();
            this.Opacity += 0.04;
        }

        private void frmSDTHITitle_Load(object sender, EventArgs e)
        {
            FillData();
            CalScoreAndSub();
            timer1.Start();
        }
    }
}
